//
//  SHEmoticonPageView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/9.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

// cell可重用标识符
private let reuseIdentifier = "emoticonPageViewCellId"
class SHEmoticonPageView: UICollectionView {
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 注册cell
        registerClass(SHEmoticonPageViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        // 获取layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = CGSize(width: K_SCREEN_WIDTH, height: 216 - 37)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 滚动方向
        layout.scrollDirection = .Horizontal
        dataSource = self
    }
    
}

extension SHEmoticonPageView: UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return SHEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SHEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SHEmoticonPageViewCell
        cell.emoticons = SHEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        cell.backgroundColor = backgroundColor
        
        return cell
    }
}

